#define _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
#include <list>
using namespace std;

int ysf(int n, int m)
{
    // write code here
    list<int> li;
    for (int i = 1; i <= n; i++)
    {
        li.push_back(i);
    }

    auto it = li.begin();
    int count = 1;
    while (it != li.end())
    {
        if (count == m)
        {
            it = li.erase(it);
            count = 1;
        }
        else
        {
            ++count;
            if ((it++) == li.end())
            {
                it = li.begin();
            }
            else
            {
                ++it;
            }
        }
        if (li.size() == 1)
        {
            break;
        }
    }

    return *(li.begin());
}
int main()
{
    cout << ysf(5, 2);
	return 0;
}